@REM Copyright (C) Microsoft Corporation. All rights reserved.

setlocal enabledelayedexpansion

  
@REM The path to the WiX directory
set WiXPath=%~1

@REM The Product_Guid is stored in the MicrosoftForumsClient.wxs file and the MicrosoftForumsClient Installer Design Document
set PRODUCT_GUID={661DEC66-478B-4c99-B702-62052415C5E2}

@REM Setup MSI File
set SETUP_MSI="%~dp0MicrosoftForumsClientSetup.msi"

@REM WixObj File
set WIX_OBJ="%~dp0MicrosoftForumsClient.wixobj"

@REM Program Files directory
set PF="%ProgramFiles%"

@REM Delete the previous MSI, if present.
if exist %SETUP_MSI% (
del /F %SETUP_MSI% )

@REM Attempt to uninstall MicrosoftForumsClient - will fail if MicrosoftForumsClient isn't already installed.
msiexec /quiet /x %PRODUCT_GUID%
if "!ERRORLEVEL!"=="0" (
  echo "MicrosoftForumsClient uninstalled successfully using the Product GUID"
) else (
  echo "MicrosoftForumsClient uninstall failed using the Product GUID" 
)

@REM Compile the wxs file
"!WixPath!\candle.exe" -out %WIX_OBJ% "%~dp0MicrosoftForumsClient.wxs"
if "!ERRORLEVEL!"=="0" (
  echo "Candle generated %WIX_OBJ%"
) else (
  echo "Candle returned !ERRORLEVEL!"
  exit /b !ERRORLEVEL!
)

@REM Build the installer
"!WixPath!\light.exe" -out %SETUP_MSI% %WIX_OBJ% "!WixPath!\lib\wixui_FeatureTree.wixlib" -loc "!WixPath!\lib\WixUI_en-us.wxl"
if "!ERRORLEVEL!"=="0" (
  echo "Light generated %SETUP_MSI%"
) else (
  echo "Light returned !ERRORLEVEL!"
  exit /b !ERRORLEVEL!
)

@REM Install MicrosoftForumsClient
msiexec /quiet /i %SETUP_MSI%
if "!ERRORLEVEL!"=="0" (
  echo "MicrosoftForumsClient installed successfully"
) else (
  echo "MicrosoftForumsClient failed to install" 
  exit /b !ERRORLEVEL!
)


@REM Get the Program Files path name correct across x86 and 64 bit platforms
if not "%PROCESSOR_ARCHITECTURE%"=="x86" (
  set PF="%ProgramFiles(x86)%"
)
  
@REM Check if MicrosoftForumsClient.exe is actually installed.
if not exist "!PF!\Developer Community Labs\Microsoft Forums Client\ForumsClient.exe" (
  echo "MicrosoftForumsClient installer claimed success but the ForumsClient.exe is not present."
  echo "at !PF!\Developer Community Labs\Microsoft Forums Client\"
  exit /b 1
)

exit /b !ERRORLEVEL!
